<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_leave_retire_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="进度" prop="progress">
        <el-select v-model="queryParams.progress" placeholder="请选择进度" clearable>
          <el-option
            v-for="dict in dict.type.sys_access_progress"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
    <el-row  :gutter="20">
      <el-col :span="12">
        <div class="grid-content bg-purple">
          
          <el-button  @click="openRetireDialog" style="width: 100%;height: 100px;font-weight: bolder;font-size: larger;">退休申请</el-button>
          
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content bg-purple-light">
          
            <el-button  @click="openLeaveDialog"  style="width: 100%;height: 100px;font-weight: bolder;font-size: larger;">辞职申请</el-button>
        </div>
        
      </el-col>
    </el-row>

    <el-row :gutter="20" style="margin-top: 10px;">
      <el-col :span="12">
        <el-card>
          <div id="pieBox" style="height: 300px;"></div>
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card>
          <div id="lineBox" style="height: 300px;"></div>
        </el-card>
      </el-col>
    </el-row>
    <!-- <el-row :gutter="10" class="mb8" style="margin-top: 15px;">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:leaveRetire:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:leaveRetire:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:leaveRetire:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:leaveRetire:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="leaveRetireList" :row-class-name="tableRowClassName" style="margin-top: 15px;">
      <!-- <el-table-column label="编号ID" align="center" prop="id" /> -->
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_leave_retire_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="原因" align="center" prop="reason" />
      <el-table-column label="附件" align="center" prop="reasonFile">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDownload(scope.row.reasonFile)"
          >下载</el-button>
        </template>
      </el-table-column>
      <el-table-column label="在职时间" align="center" prop="workingTime" />
      <el-table-column label="开始工作时间" align="center" prop="startWork" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startWork, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束工作时间" align="center" prop="endWork" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endWork, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进度" align="center" prop="progress">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_access_progress" :value="scope.row.progress"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['system:leaveRetire:edit']"
            v-hasRole="['admin', 'school']"
            :disabled="scope.row.progress === '2'"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:leaveRetire:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 辞职弹窗 -->
    <el-dialog :title="title" :visible.sync="leaveOpen" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="原因" prop="reason">
          <el-input v-model="form.reason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="附件" prop="reasonFile">
          <!-- <file-upload v-model="form.reasonFile"/>
           -->
           <el-upload
              class="upload-demo"
              drag
              v-model="form.reasonFile"
              action="https://jsonplaceholder.typicode.com/posts/"
              multiple>
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <!-- <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div> -->
            </el-upload>
        </el-form-item>
        <el-form-item label="结束工作时间" prop="endWork">
          <el-date-picker clearable
            v-model="form.endWork"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束工作时间">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="进度" prop="progress">
          <el-select v-model="form.progress" placeholder="请选择进度">
            <el-option
              v-for="dict in dict.type.sys_access_progress"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 退休弹窗 -->
    <el-dialog :title="title" :visible.sync="retireOpen" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="原因" prop="reason">
          <el-input v-model="form.reason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="附件" prop="reasonFile">
          <!-- <file-upload v-model="form.reasonFile"/> -->
          <!-- <el-upload
              class="upload-demo"
              drag
              v-model="form.reasonFile"
              action="https://jsonplaceholder.typicode.com/posts/"
              multiple>
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>

            </el-upload> -->
            <file-upload v-model="form.reasonFile"/>
        </el-form-item>
        <el-form-item label="结束工作时间" prop="endWork">
          <el-date-picker clearable
            v-model="form.endWork"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束工作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



  </div>
</template>

<script>
import { getLeaveRetireAverageTime,listLeaveRetire, getLeaveRetire, delLeaveRetire, addLeaveRetire, updateLeaveRetire,getLeaveRetirePeopleNum } from "@/api/system/leaveRetire";
import * as echarts from 'echarts';

export default {
  name: "LeaveRetire",
  dicts: ['sys_access_progress', 'sys_leave_retire_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 离退休申请表格数据
      leaveRetireList: [],
      leaveRetire: null,
      // 弹出层标题
      title: "",
      // 退休弹窗
      retireOpen: false,
      // 辞职弹窗
      leaveOpen: false,
      // 审核弹窗
      auditOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        progress: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  mounted(){

    var pieDom = document.getElementById('pieBox');
    var pieChart = echarts.init(pieDom);

    var lineDom = document.getElementById('lineBox');
    var lineChart = echarts.init(lineDom);


    const pie_option = {
      tooltip: {
        trigger: 'item'
      },
      legend: {
        top: '5%',
        left: 'center'
      },
      series: [
        {
          name: '人数',
          type: 'pie',
          radius: ['40%', '70%'],
          avoidLabelOverlap: false,
          itemStyle: {
            borderRadius: 10,
            borderColor: '#fff',
            borderWidth: 2
          },
          label: {
            show: false,
            position: 'center'
          },
          emphasis: {
            label: {
              show: true,
              fontSize: 40,
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: false
          },
          data: [
            // { value: 1048, name: 'Search Engine' },
            // { value: 735, name: 'Direct' },
            // { value: 580, name: 'Email' },
            // { value: 484, name: 'Union Ads' },
            // { value: 300, name: 'Video Ads' }
          ]
        }
      ]
    };

    const line_option = {
      xAxis: {
        type: 'category',
        data: []
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          data: [

          ],
          type: 'bar'
        }
      ]
    };

    

    getLeaveRetirePeopleNum().then(res => {
      pie_option.series[0].data = res.data;
      pieChart.setOption(pie_option);
    })
    
    getLeaveRetireAverageTime().then(res => {
      line_option.xAxis.data = res.data.map(item => item.name)
      line_option.series[0].data = res.data.map(item => item.value)
      lineChart.setOption(line_option);
    })
  },
  created() {
    this.getList();
  },
  methods: {
    tableRowClassName({row, rowIndex}) {
        if (row.type == '1') {
          return 'warning-row';
        } else if (row.type == '0') {
          return 'success-row';
        }
        return '';
      },
    /** 查询离退休申请列表 */
    getList() {
      this.loading = true;
      listLeaveRetire(this.queryParams).then(response => {
        this.leaveRetireList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.leaveOpen = false;
      this.retireOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        type: null,
        reason: null,
        reasonFile: null,
        workingTime: null,
        startWork: null,
        endWork: null,
        progress: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    openRetireDialog() {
      this.retireOpen = true;
      this.form.type = '0'
      this.title = "退休申请表";
    },
    openLeaveDialog() {
      console.log('Opening leave dialog');
      this.leaveOpen = true;
      this.form.type = '1'
      this.title = "辞职申请表";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加离退休申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLeaveRetire(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改离退休申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLeaveRetire(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.retireOpen = false;
              this.leaveOpen = false;
              this.getList();
            });
          } else {
            addLeaveRetire(this.form).then(response => {
              this.$modal.msgSuccess("提交成功");
              this.retireOpen = false;
              this.leaveOpen = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除离退休申请编号为"' + ids + '"的数据项？').then(function() {
        return delLeaveRetire(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/leaveRetire/export', {
        ...this.queryParams
      }, `leaveRetire_${new Date().getTime()}.xlsx`)
    },
    handleAudit(row) {
      // this.auditOpen = true;
      this.$confirm('是否确定通过, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '成功!'
          });
          getLeaveRetire(row.id).then(response => {
            this.leaveRetire = response.data;

            this.leaveRetire.progress = "2"
          updateLeaveRetire(this.leaveRetire).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.retireOpen = false;
              this.leaveOpen = false;
              this.getList();
            });
          });
          // this.leaveRetire.userId = row.userId
          // this.leaveRetire.progress = "2"
          // updateLeaveRetire(this.leaveRetire).then(response => {
          //     this.$modal.msgSuccess("修改成功");
          //     this.retireOpen = false;
          //     this.leaveOpen = false;
          //     this.getList();
          //   });

          
        }).catch(() => {});
        // this.getList();
        // .catch(() => {
        //   this.$message({
        //     type: 'info',
        //     message: '取消'
        //   });          
        // });
    }
  }
};
</script>


<style>
  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }

</style>